<?php

namespace App\Http\Controllers\Personal;
use App\Models\Company\CompanyUser;
use App\Models\Personal\Resume;
use App\Models\Personal\ShieldCompanies;
use Illuminate\Http\Request;
use Style;

class ShieldCompaniesController extends CommonController{
    /**
     * 隐私设置
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function privacy(Request $request){
        $user = auth()->guard('personal')->user();
        if(!empty($request->resume_id)){
            $resume = Resume::where('uid',$user->uid)->where('id', $request->resume_id)->first();
        }else{
            $resume = Resume::where('uid',$user->uid)->where('default', 1)->first();
        }
        if(!empty($resume)){
            $seachCom = [];
            $companycount = '';
            $shieldId = [];
            //屏蔽企业
            $shieldArr = [];
            $shieldcompanies  = Shieldcompanies::where('personal_uid',$user->uid)->where('resume_id',$resume->id)->get();//实例化User表
            if(count($shieldcompanies) > 0 ){
                foreach ($shieldcompanies as $shield) {
                    $shieldArr[] = array(
                        "company_name"=>$shield->shieldComMany->company_name,
                        "company_uid"=>$shield->company_uid,
                        "id"=>$shield->id
                    );
                    $shieldId[]=$shield->company_uid;
                }
            }
            //是不是设置了简历
            $set_status = '';
            //是不是设置简历
            if($request->type == 'up'){
                $resume_status = $request->resume_status;
                if($resume->resume_status == '1'|| $resume->resume_status == '2' || $resume->resume_status  == '3'){
                    $resume->resume_status = $resume_status;
                }
                $resume->bkresume_status = $resume_status;
                $resume->xs_status = 0;
                $resume->save();
                //保密设置提示信息
                return redirect('personal/privacy?resume_id='.$resume->id)->with('ok',"隐私设置成功");
            }else{
                $resume_status = $resume->bkresume_status;
                //搜索关键字
                if($request->type == 'sql'){
                    $keyword = $request->keyword;
                    if(!empty($keyword)){
                        $currtime = date('Y-m-d H:i:s',time());
                        $CompanyUser = CompanyUser::where('company_name','like','%'.$keyword.'%')->limit(10)->get();
                        if(count($CompanyUser) > 0 ) {
                            foreach ($CompanyUser as $v) {
                                if (!in_array($v->uid, $shieldId)) {
                                    $seachCom[] = array(
                                        "company_name" => $v->company_name,
                                        "company_uid" => $v->uid,
                                    );
                                }
                            }
                        }
                        $companycount = (count($CompanyUser)) ? count($CompanyUser) : 'null';
                    }
                }
            }
            //dd($shieldArr);
            return Style::view('personal.privacy',compact('resume','resume_status','shieldArr','seachCom','companycount'));
        }else{
            return Style::view('personal.warmtips');
        }
    }

    /**
     * 添加屏蔽企业
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addShield(Request $request){
        $user = auth()->guard('personal')->user();
        $resume = Resume::where('uid',$user->uid)->first();
        $input = $request->only(['company_uid']);
        //获取用户信息ID
        $input['personal_uid'] = $user->uid;
        $input['resume_id'] = $resume->id;
        $input['addtime'] = date('Y-m-d H:i:s',time());
        //返回插入的最后ID
        $id = Shieldcompanies::insertGetId($input);
        if($id){
            return response()->json(['status' => 0,'msg' => '添加屏蔽企业成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '添加屏蔽企业失败，请稍后重试！']);
        }
    }

    /**
     * 删除屏蔽企业
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delShield(Request $request){
        $user = auth()->guard('personal')->user();
        $resume = Resume::where('uid',$user->uid)->first();
        $result = Shieldcompanies::where('company_uid',$request->company_uid)->where('personal_uid',$user->uid)->where('resume_id',$resume->id)->delete();
        if($result){
            return response()->json(['status' => 0,'msg' => '删除屏蔽企业成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '删除屏蔽企业失败，请稍后重试！']);
        }
    }
}